{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天？？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def day_of_year(year, month, day):\n",
    "    # 每个月的天数，非闰年情况\n",
    "    days_per_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    # 判断是否是闰年\n",
    "    if (year % 4 == 0 and year % 100!= 0) or (year % 400 == 0):\n",
    "        days_per_month[2] = 29\n",
    "    total_days = 0\n",
    "    for m in range(1, month):\n",
    "        total_days += days_per_month[m]\n",
    "    return total_days + day\n",
    "# 获取用户输入\n",
    "year, month, day = map(int, input(\"请输入年 月 日，格式为yyyy mm dd：\").split())\n",
    "# 调用函数计算并输出结果\n",
    "print(day_of_year(year, month, day))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.判断1000中有多少个素数，并输出所有素数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1到1000中的素数有 168 个\n",
      "这些素数是： [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(num):\n",
    "    if num <= 1:\n",
    "        return False\n",
    "    if num <= 3:\n",
    "        return True\n",
    "    if num % 2 == 0 or num % 3 == 0:\n",
    "        return False\n",
    "    i = 5\n",
    "    while i * i <= num:\n",
    "        if num % i == 0 or num % (i + 2) == 0:\n",
    "            return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "prime_numbers = []\n",
    "for n in range(1, 1000):\n",
    "    if is_prime(n):\n",
    "        prime_numbers.append(n)\n",
    "\n",
    "print(\"1到1000中的素数有\", len(prime_numbers), \"个\")\n",
    "print(\"这些素数是：\", prime_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该同学的成绩等级是： B\n"
     ]
    }
   ],
   "source": [
    "score = int(input(\"请输入学习成绩：\"))\n",
    "grade = \"A\" if score >= 90 else (\"B\" if score >= 60 else \"C\")\n",
    "print(\"该同学的成绩等级是：\", grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数： 17\n",
      "空格个数： 4\n",
      "数字个数： 17\n",
      "其他字符个数： 1\n"
     ]
    }
   ],
   "source": [
    "s = input(\"请输入一行字符串：\")\n",
    "letters = 0\n",
    "spaces = 0\n",
    "digits = 0\n",
    "others = 0\n",
    "for char in s:\n",
    "    if char.isalpha():\n",
    "        letters += 1\n",
    "    elif char.isspace():\n",
    "        spaces += 1\n",
    "    elif char.isdigit():\n",
    "        digits += 1\n",
    "    else:\n",
    "        others += 1\n",
    "print(\"英文字母个数：\", letters)\n",
    "print(\"空格个数：\", spaces)\n",
    "print(\"数字个数：\", digits)\n",
    "print(\"其他字符个数：\", others)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 是完数，其因子为： [1, 2, 3]\n",
      "28 是完数，其因子为： [1, 2, 4, 7, 14]\n",
      "496 是完数，其因子为： [1, 2, 4, 8, 16, 31, 62, 124, 248]\n"
     ]
    }
   ],
   "source": [
    "for num in range(1, 1001):\n",
    "    factors = []\n",
    "    for i in range(1, num):\n",
    "        if num % i == 0:\n",
    "            factors.append(i)\n",
    "    if sum(factors) == num:\n",
    "        print(num, \"是完数，其因子为：\", factors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! +... + 13 !的值为： 6749977113\n"
     ]
    }
   ],
   "source": [
    "from decimal import Decimal, getcontext\n",
    "\n",
    "# 设置精度，这里设置为足够大的精度以满足计算需求\n",
    "getcontext().prec = 1000\n",
    "\n",
    "N = int(input(\"请输入任意数N：\"))\n",
    "\n",
    "total = Decimal(0)\n",
    "factorial = Decimal(1)\n",
    "\n",
    "for i in range(1, N + 1):\n",
    "    factorial *= Decimal(i)\n",
    "    total += factorial\n",
    "\n",
    "print(\"1 + 2! + 3! +... +\", N, \"!的值为：\", total)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "    *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   *\n",
      "  ***\n",
      " *****\n",
      "*******\n",
      " *****\n",
      "  ***\n",
      "   *\n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入任意奇数N：\"))\n",
    "\n",
    "# 上半部分菱形\n",
    "for i in range(1, (N + 1) // 2 + 1):\n",
    "    # 打印空格\n",
    "    for j in range((N - 1) // 2 - (i - 1)):\n",
    "        print(\" \", end=\"\")\n",
    "    # 打印星号\n",
    "    for k in range(2 * i - 1):\n",
    "        print(\"*\", end=\"\")\n",
    "    print()\n",
    "\n",
    "# 下半部分菱形\n",
    "for i in range((N + 1) // 2 - 1, 0, -1):\n",
    "    # 打印空格\n",
    "    for j in range((N - 1) // 2 - (i - 1)):\n",
    "        print(\" \", end=\"\")\n",
    "    # 打印星号\n",
    "    for k in range(2 * i - 1):\n",
    "        print(\"*\", end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.对10个数进行排序。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入10个数：\n",
      "排序后的结果： [5.0, 8.0, 145.0, 155.0, 258.0, 415.0, 475.0, 1456.0, 47478.0, 95225.0]\n"
     ]
    }
   ],
   "source": [
    "nums = []\n",
    "print(\"请输入10个数：\")\n",
    "for i in range(10):\n",
    "    num = float(input())\n",
    "    nums.append(num)\n",
    "\n",
    "sorted_nums = sorted(nums)\n",
    "print(\"排序后的结果：\", sorted_nums)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组： [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 88.0]\n"
     ]
    }
   ],
   "source": [
    "# 假设已经有一个排好序的数组\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]\n",
    "\n",
    "# 从键盘获取要插入的数\n",
    "num_to_insert = float(input(\"请输入一个数：\"))\n",
    "\n",
    "# 找到插入位置\n",
    "insert_index = 0\n",
    "for i, num in enumerate(sorted_array):\n",
    "    if num_to_insert <= num:\n",
    "        insert_index = i\n",
    "        break\n",
    "    insert_index = len(sorted_array)\n",
    "\n",
    "# 插入数到数组中\n",
    "sorted_array.insert(insert_index, num_to_insert)\n",
    "\n",
    "print(\"插入后的数组：\", sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "19.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第648号的那位\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入总人数n：\"))\n",
    "\n",
    "people = list(range(1, n + 1))\n",
    "index = 0\n",
    "while len(people) > 1:\n",
    "    count = 0\n",
    "    while count < 3:\n",
    "        if people[index % len(people)]!= 0:\n",
    "            count += 1\n",
    "        if count == 3:\n",
    "            people[index % len(people)] = 0\n",
    "        index += 1\n",
    "    people = [p for p in people if p!= 0]\n",
    "\n",
    "print(\"最后留下的是原来第{}号的那位\".format(people[0]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
